package com.dao.qsl;

import com.bean.qsl.CfInfo;
import com.util.qsl.Dbutil;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class WpddDao {
      public List<CfInfo> wp(HttpServletRequest request ){
	  Dbutil d=new Dbutil();
	  Connection c=null;

	  List<CfInfo> list=new ArrayList<>();

	  Map<String,Object> user = (Map<String,Object>)request.getSession().getAttribute("user");
	  int eid = (int) user.get("eid");
	  System.out.println("eid:"+eid);

	  try{
	        c=d.getConnection();
	        String sql="SELECT a.appoint_id ,h.hname,m.maintain_content ,m.maintain_phone ," +
					"a.appoint_servetime from appoint a,maintain m ,household h where h.aid=m.aid and" +
					"  m.maintain_id=a.maintain_id and  eid=?";
	        PreparedStatement pr=c.prepareStatement(sql);

	        pr.setInt(1,eid);

	        ResultSet rs=pr.executeQuery();

	        while (rs.next()){
		    CfInfo cf=new CfInfo();

		    cf.setWpbh(rs.getString("appoint_id"));
		    cf.setSqrname(rs.getString("hname"));
		    cf.setSqlr(rs.getString("maintain_content"));
		    cf.setLxdh(rs.getString("maintain_phone"));
		    cf.setWpsj(rs.getString("appoint_servetime"));

		    list.add(cf);
	        }


	  }catch (Exception e){
	        e.printStackTrace();
	  }
	  return list;

      }

      /**
       * 委派后修改报修订单状态
       * @param bh
       */
      public  Boolean xgzt(int bh){
	  Dbutil d=new Dbutil();

	  Connection c=null;

	  try {
	        c=d.getConnection();

	        String sql="UPDATE maintain set maintain_status='A' where maintain_id=?";

	        PreparedStatement pr=c.prepareStatement(sql);

	        pr.setInt(1,bh);

	        int i=pr.executeUpdate();

	        if(i>0){
		    return true;
	        }else{
		    return false;
	        }

	  }catch (Exception e){
	        e.printStackTrace();
	  }
	  return false;
      }

      /**
       * 委派维修订单
       */
      public  boolean wxddwp(String wpname , int bh, String lxdh ,HttpServletRequest request ){
	  Dbutil d=new Dbutil();
	  Connection c=null;

	  //添加 appoint 表数据   委派id(自增),申请人id，申请类容，联系电话(前端返回),委派时间(数据库自动生成NOW()) ,处理人id


	   List<String> list= getsj(bh);

	  //申请人姓名
	  String sqrid=null;
	  if (list != null && list.size() > 0) {
	        sqrid=list.get(0);
	  }
	  System.out.println("申请人姓名:"+sqrid);

	  //报修类容
	  String bxlr=null;
	  if (list != null && list.size() > 0) {
	        bxlr=list.get(1);
	  }
	  System.out.println("报修类容:"+bxlr);


	  //处理人id
	  int clrid=clrid(wpname);

	  if(clrid==0){
	        return false;
	  }
	  System.out.println("处理人id:"+wpname);
	  try {
	        c=d.getConnection();

	        String sql="INSERT INTO appoint VALUES(null,?,?,?,NOW(),?)";

	        PreparedStatement pr=c.prepareStatement(sql);

	        pr.setInt(1,bh);
	        pr.setString(2,sqrid);
	        pr.setString(3,bxlr);
	        pr.setInt(4,clrid);

	        int i=pr.executeUpdate();

	        if(i>0){
		    return true;
	        }else {
		    return false;
	        }

	  }catch(Exception e){
	        e.printStackTrace();
	  }
	  return false;
      }


      /**
       * 申请报修订单编号，联系电话，委派订单编号 ,申请人id
       */

      public  List<String> getsj(int bh){
	  Dbutil d=new Dbutil();

	  Connection c=null;
	  List<String>  list=new ArrayList<>();


	  try {
	        c=d.getConnection();

	        //获取 申请人id，申请类容

	        String sql="SELECT m.*,h.Hname from maintain m,household h where  m.aid=h.aid and maintain_id=?";

	        System.out.println(sql);
	        PreparedStatement pr=c.prepareStatement(sql);

	        pr.setInt(1,bh);

	 ResultSet rs=pr.executeQuery();

	 if(rs.next()){
	       list.add(rs.getString("aid")); //申请人id
	       System.out.println("申请人id"+rs.getString("aid"));
	       list.add(rs.getString("maintain_content")); //申请类容
	       System.out.println("申请类容:"+rs.getString("maintain_content"));
	 }
	  }catch(Exception e){

	  }
	  return  list;
      }

      /**
       * 获取处理人id
       */
      public  int clrid(String wpname){
	  Dbutil d=new Dbutil();
	  Connection c=null;


	  try{
	        c=d.getConnection();
	        String sql="SELECT eid from  emp where ename=?";

	        PreparedStatement pr=c.prepareStatement(sql);

	        pr.setString(1, wpname);

	        ResultSet rs=pr.executeQuery();

	        if (rs.next()){
		    Integer  id=rs.getInt("eid");
		    return id;
	        }
	  }catch (Exception e){
	        e.printStackTrace();
	  }
	  return 0;
      }

/**
 * 处理委派的订单
 *
 */
public  Boolean clwp(int wpbh){
      Dbutil d=new Dbutil();
      Connection c=null;

      try {
	c=d.getConnection();
	String sql="UPDATE maintain set maintain_status='C' where maintain_id=(SELECT  maintain_id from appoint where appoint_id=?)";
     	PreparedStatement pr=c.prepareStatement(sql);
	     pr.setInt(1,wpbh);

	     int i=pr.executeUpdate();
	     if(i>0){
		 return true;
	     }else{
		 return false;
	     }
      }catch(Exception e){
	  e.printStackTrace();
      }
      return false;

}

/**
 * 处理完成删除委派订单
 */
public  Boolean  scdd(int wpbh){
      Dbutil d=new Dbutil();

      Connection c=null;

      try {
	  c=d.getConnection();
	  String sql="DELETE from appoint where appoint_id=?";
	  PreparedStatement pr=c.prepareStatement(sql);
	 pr.setInt(1,wpbh);

	 int i=pr.executeUpdate();
	 if(i>0){
	       return true;
	 }else{
	       return false;
	 }
      }catch(Exception e){
	  e.printStackTrace();
      }
      return false;
}
}
